import { Toast } from "antd-mobile"
import axios from "axios"

const request = axios.create({
  baseURL: window?.alignConfig?.kugou || "",
})
request.interceptors.response.use(
  function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response
  },
  function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    Toast.show({
      content: error?.response?.data || "请求错误",
    })
    return Promise.reject(error)
  },
)

const api = {
  search: "/search",
  song: "/song/url",
  getQrKey: "/login/qr/key",
  createQr: "/login/qr/create",
  checkQrState: "/login/qr/check",
}

export interface SearchParams {
  keyword: string
  page?: string
  pageSize?: string
  type?: "special" | "lyric" | "song" | "album" | "author" | "mv"
}
export function search(keywords: string, params = {}) {
  return request({
    url: api.search,
    params: {
      keywords,
      ...params,
    },
  })
}
export function song(hash?: string) {
  return request({
    url: api.song,
    params: {
      hash,
    },
  })
}

export function getQrKey() {
  return request({
    url: api.getQrKey,
    params: { time: new Date() },
  })
}
export function createQr(key?: any) {
  return request({
    url: api.createQr,
    params: { time: new Date(), key },
  })
}
export function checkQrState(key: string) {
  return request({
    url: api.checkQrState,
    params: { time: new Date(), key },
  })
}
